import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/account/controller/xindong_register_ctr.dart';
import 'package:xindong/utils/xindong_image_util.dart';

class XinDongEditGenderView extends StatelessWidget {
  const XinDongEditGenderView({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          height: 40,
          width: screenWidth / 2,
          padding: const EdgeInsets.all(1),
          decoration: BoxDecoration(
            color: DZColors.dividerColor,
            borderRadius: BorderRadius.circular(16),
          ),
          child: GetBuilder<XinDongRegisterCtr>(builder: (controller) {
            return Row(
              children: [
                _buildIconItem(
                  iconName: AssetsRes.ICON_SEX_MAN,
                  isSelect: controller.registerClass.sex == Gender.male,
                  onTap: () {
                    controller.registerClass.sex = Gender.male;
                    controller.update();
                  },
                ),
                const SizedBox(width: 1),
                _buildIconItem(
                  iconName: AssetsRes.ICON_SEX_WOMAN,
                  isSelect: controller.registerClass.sex == Gender.female,
                  onTap: () {
                    controller.registerClass.sex = Gender.female;
                    controller.update();
                  },
                ),
              ],
            );
          }),
        ),
        SizedBox(
          height: 40,
          width: screenWidth / 2,
          child: GetBuilder<XinDongRegisterCtr>(builder: (controller) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildNameItem(name: "男生", isSelect: controller.registerClass.sex == Gender.male),
                _buildNameItem(name: '女生', isSelect: controller.registerClass.sex == Gender.female),
              ],
            );
          }),
        ),
        const Text(
          "性别一经确认后续无法修改哦",
          style: TextStyle(color: DZColors.placeholderColor, fontSize: 13),
        )
      ],
    );
  }

  Widget _buildIconItem({required String iconName, required bool isSelect, required Function() onTap}) {
    final borderRadius = iconName == AssetsRes.ICON_SEX_MAN
        ? const BorderRadius.horizontal(left: Radius.circular(15))
        : const BorderRadius.horizontal(right: Radius.circular(15));
    return Expanded(
      flex: 1,
      child: GestureDetector(
        onTap: onTap,
        behavior: HitTestBehavior.opaque,
        child: Container(
          height: double.infinity,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: isSelect ? DZColors.coverColor : DZColors.white,
            borderRadius: borderRadius,
          ),
          child: ImageUtil.asset(iconName, width: 20, height: 20),
        ),
      ),
    );
  }

  Widget _buildNameItem({required String name, required bool isSelect}) {
    return Text(name, style: TextStyle(color: isSelect ? DZColors.mainColor : DZColors.titleColor, fontSize: 13));
  }
}
